home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / editor / we16_95g.zip / WEEXTSRC.ZI_ / WE_EXT.C < prev    next >
C/C++ Source or Header  |  1994-10-24  |  12KB  |  365 lines

  1. /*-------------------------------------------------------------------------*\
  2.  |                                                                         |
  3.  |                                                                         |
  4.  |  WE_EXT.C - A Sample DLL Extension Processor for WinEdit                |
  5.  |                                                                         |
  6.  |                                                                         |
  7. \*-------------------------------------------------------------------------*/
  8. #define STRICT
  9. #define _WINDLL
  10. #include <windows.h>
  11. #include "we_ext.h"
  12. #include "private.h"
  13.  
  14. #define NOREF(a)  {a=a;}
  15.  
  16. HMENU hTrackMenu;
  17. BOOL bWait=TRUE;
  18. BOOL bCapture=TRUE;
  19. char szCommand[256];
  20.  
  21. #if !defined(LONG2POINT)
  22. #define LONG2POINT(l,pt) ((pt).x=(SHORT)LOWORD(l), (pt).y=(SHORT)HIWORD(l))
  23. #endif
  24.  
  25. /*-------------------------------------------------------------------------*\
  26.  |                                                                         |
  27.  |  Function:   WE_ExtensionProc                                           |
  28.  |                                                                         |
  29.  |  Purpose:    WinEdit calls this function with the WEN_* messages and    |
  30.  |              whenever a user-defined menu item or accelerator is        |
  31.  |              accessed.                                                  |
  32.  |                                                                         |
  33.  |  Parameters: HWND hWnd   - WinEdit's window handle                      |
  34.  |                                                                         |
  35.  |              UINT wParam - Message ID.  If wParam is >= WE_EXTFIRST,    |
  36.  |                            the DLL is being requested to perform the    |
  37.  |                            user-defined action.                         |
  38.  |                                                                         |
  39. \*-------------------------------------------------------------------------*/
  40. UINT FAR PASCAL WE_ExtensionProc(HWND hWnd,     /* WinEdit's window handle */
  41.                                  HANDLE hInst,  /* instance identifier     */
  42.                                  UINT wParam,   /* command ID              */
  43.                                  LONG lParam)   /* additional information  */
  44.    {
  45.  
  46.    switch (wParam)
  47.       {
  48.  
  49.       case WEN_LOADMENU:
  50.  
  51.          /*  This is the menu WinEdit will display when there
  52.           *  is at least one document window open.  Return NULL
  53.           *  to use the default WinEdit menu.
  54.           *
  55.           */
  56.  
  57.          return (UINT)LoadMenu(hInst, "MyMenu");
  58.          break;
  59.  
  60.       case WEN_LOADSHORTMENU:
  61.  
  62.          /*  this is the menu WinEdit will display when there
  63.              *  are no document windows open.  Return NULL
  64.              *  to use the default WinEdit menu.
  65.              *
  66.              */
  67.    
  68.             return (UINT)LoadMenu(hInst, "MyShortMenu");
  69.             return 0;
  70.             break;
  71.    
  72.       case WEN_LOADACCELS:
  73.  
  74.          /*  To re-define the WinEdit command keys, load your
  75.           *  own accelerator table here.  Return NULL to
  76.           *  use the default WinEdit accelerators.
  77.           *
  78.           */
  79.  
  80.          return (UINT)LoadAccelerators (hInst,"MyAccels");
  81.          break;
  82.  
  83.       case WEN_GETWINDOWMENU:
  84.  
  85.          /*  WinEdit needs the handle of the submenu to
  86.           *  append MDI document names to.  The hWnd parameter
  87.           *  is used to send the handle to the main menu.
  88.           *  This message will not be sent if you return
  89.           *  NULL to the WEN_LOADMENU message.
  90.           */
  91.          return (UINT)GetSubMenu ((HMENU)hWnd, WINDOWMENU);
  92.          break;
  93.  
  94.          /*  WinEdit sends right button clicks to the
  95.           *  extension processor.  lParam contains the
  96.           *  x and y coordinates of the mouse cursor,
  97.           *  in screen coordinates.
  98.           */
  99.       case WEN_RBUTTONDOWNS:
  100.          edHelpKeyWord(hWnd);
  101.          break;
  102.  
  103.       case WEN_RBUTTONDOWN:
  104.  
  105.          if (!hTrackMenu)
  106.             {
  107.  
  108.             hTrackMenu = CreatePopupMenu();
  109.             if (!hTrackMenu)
  110.                break;
  111.                
  112.             AppendMenu(hTrackMenu,MF_STRING,IDM_FILEOPEN,"&Open...\tF3");
  113.             AppendMenu(hTrackMenu,MF_STRING,IDM_FILESAVE,"&Save\tF2");                 
  114.             AppendMenu(hTrackMenu,MF_STRING,IDM_FILEPRINT,"&Print\tF9");                
  115.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  116.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITUNDO,"&Undo\tAlt+BkSp");           
  117.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITREDO,"&Redo\tCtrl+BkSp");          
  118.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  119.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITCUT,"Cu&t\tShift+Del");           
  120.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITCOPY,"&Copy\tCtrl+Ins");           
  121.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITPASTE,"&Paste\tShift+Ins");         
  122.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  123.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHFIND,"&Find...\tF5");              
  124.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHNEXT,"&Repeat Last Find\tCtrl+F5");
  125.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHCHANGE,"&Change...\tF6");            
  126.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  127.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHNEXTERR,"&Next Error\tShift+F3");    
  128.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHPREVERR,"&Previous Error\tShift+F4");
  129.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  130.             AppendMenu(hTrackMenu,MF_STRING,IDM_HELPKEYWORDS,"&Key Word Help\tShift+F1");  
  131.             }
  132.             
  133.          if (hTrackMenu)
  134.             {
  135.             POINT pt;
  136.  
  137. #ifdef WIN32
  138.             LONG2POINT(lParam,pt);
  139. #else
  140.             pt = MAKEPOINT(lParam);
  141. #endif
  142.             TrackPopupMenu(hTrackMenu,0,pt.x-10,pt.y-6,0,hWnd,0L);
  143.             }
  144.             
  145.          return TRUE;   
  146.          break;
  147.  
  148.       case WEN_END:
  149.  
  150.          /*  WinEdit is shutting down.  Do any clean-up processing
  151.           *  here.
  152.           */
  153.          if (hTrackMenu)
  154.             {
  155.             DestroyMenu(hTrackMenu);
  156.             hTrackMenu = (HMENU)NULL;
  157.             }
  158.          return TRUE;
  159.          break;
  160.  
  161.       case WEN_INITMENU:
  162.  
  163.          /*  This message is sent before showing any drop down
  164.           *  menu items.  Respond by setting any checkmarks,
  165.           *  graying any inapplicable items, etc.
  166.           *
  167.           */
  168.          return InitMenu(hWnd);
  169.          break;
  170.  
  171.  
  172.       /*  You can define your own commands in the range
  173.        *  WE_EXTFIRST to WE_EXTLAST that can be attached to
  174.        *  menu items or accelerators.
  175.        */
  176.  
  177.       case EXT_EXAMPLE:
  178.          {
  179.          char szWord[64];
  180.          edEditGetCurrentWord(hWnd,szWord,63);
  181.          if (szWord[0])
  182.             wsprintf(szCommand,"tee.com fgrep.com -M %s *.c",(LPSTR)szWord);
  183.          if (DialogBox(hInst,"CommandBox",hWnd,CommandDlgProc))
  184.             edRunCommand(hWnd, bWait, bCapture, szCommand);
  185.          return TRUE;
  186.          break;
  187.          }
  188.  
  189.       case EXT_FOR:
  190.          {
  191.          /* Ctrl+F:  'C' template for   for ( ; ; )
  192.  
  193.           */
  194.          UINT wColNo;
  195.  
  196.          edEditInsertString(hWnd,"for ( ; ; )");
  197.  
  198.          wColNo  = edGetColumnNumber(hWnd);
  199.  
  200.          edEditGoToColumn(hWnd,wColNo-5);
  201.  
  202.          return TRUE;
  203.          break;
  204.          }
  205.  
  206.       case EXT_SWITCH:
  207.          {
  208.          /* Ctrl+S:  'C' template for   switch (  )
  209.                                            {
  210.  
  211.                                            }
  212.           */
  213.          UINT wColNo;
  214.          UINT wLineNo;
  215.  
  216.          edEditInsertString(hWnd,"switch (  )\r   {\r\r}");
  217.  
  218.          wColNo  = edGetColumnNumber(hWnd);
  219.          wLineNo = edGetLineNumber(hWnd);
  220.  
  221.          edEditGoToColumn(hWnd,wColNo+5);
  222.  
  223.          edEditGoToLine(hWnd,wLineNo-3);
  224.  
  225.          return TRUE;
  226.          break;
  227.          }
  228.  
  229.       default:
  230.  
  231.          /* return NULL to all messages not processed. */
  232.  
  233.          break;
  234.  
  235.       }  /* end switch (wParam) */
  236.  
  237.    return 0;
  238.  
  239.    }
  240.  
  241. BOOL FAR PASCAL LibMain(HANDLE hInstance, UINT wDataSeg, UINT cbHeap, LPSTR lpszCmdLine)
  242.    {
  243.    return TRUE;
  244.  
  245.    NOREF(hInstance);
  246.    NOREF(wDataSeg);
  247.    NOREF(cbHeap);
  248.    NOREF(lpszCmdLine);
  249.    }
  250.  
  251.  
  252. int FAR PASCAL WEP(int nParameter)
  253.    {
  254.    return TRUE;
  255.  
  256.    NOREF(nParameter);
  257.    }
  258.  
  259.  
  260. BOOL FAR PASCAL  CommandDlgProc(HWND hDlg, UINT msg, UINT wParam, LONG lParam)
  261.    {
  262.    switch (msg)
  263.       {
  264.       case WM_INITDIALOG:
  265.          CheckDlgButton(hDlg,IDD_WAIT,bWait);
  266.          CheckDlgButton(hDlg,IDD_CAPTURE,bCapture);
  267.          SetDlgItemText(hDlg,IDD_COMMAND,szCommand);
  268.  
  269.       case WM_COMMAND:
  270.          switch(wParam)
  271.             {
  272.             case IDOK:
  273.                bWait = IsDlgButtonChecked(hDlg,IDD_WAIT);
  274.                bCapture = IsDlgButtonChecked(hDlg,IDD_CAPTURE);
  275.                if (GetDlgItemText(hDlg,IDD_COMMAND,szCommand,sizeof(szCommand)))
  276.                   {
  277.                   EndDialog(hDlg,TRUE);
  278.                   break;
  279.                   }
  280.                /* else fall through */
  281.  
  282.             case IDCANCEL:
  283.                EndDialog(hDlg,FALSE);
  284.                break;
  285.  
  286.             default:
  287.                return (FALSE);
  288.             }
  289.          break;
  290.  
  291.       default:
  292.          return(FALSE);
  293.       }
  294.    return (TRUE);
  295.  
  296.    NOREF(lParam);
  297.    }
  298.  
  299.  
  300. int InitMenu(HWND hWnd)
  301.    {
  302.    UINT wStatus;
  303.    HMENU hCurrentMenu;
  304.    POINT ptStart,ptEnd;
  305.  
  306.    hCurrentMenu = GetMenu(hWnd);
  307.  
  308.    /*  if there is a current selection, enable the cut & copy
  309.     *  commands.
  310.     */
  311.    wStatus = (UINT)edGetSelectionState(hWnd, &ptStart, &ptEnd);
  312.    if (!wStatus)
  313.       wStatus = MF_GRAYED;
  314.    else
  315.       wStatus = MF_ENABLED;
  316.    EnableMenuItem(hCurrentMenu, IDM_EDITCUT,  wStatus);
  317.    EnableMenuItem(hCurrentMenu, IDM_EDITCOPY, wStatus);
  318.  
  319.    /*  if there is text on the clipboard, enable the paste
  320.     *  command.
  321.     */
  322.    if (OpenClipboard(hWnd))
  323.       {
  324.       if (IsClipboardFormatAvailable(CF_TEXT)
  325.             || IsClipboardFormatAvailable(CF_OEMTEXT))
  326.          EnableMenuItem(hCurrentMenu, IDM_EDITPASTE, MF_ENABLED);
  327.       else
  328.          EnableMenuItem(hCurrentMenu, IDM_EDITPASTE, MF_GRAYED);
  329.       CloseClipboard();
  330.       }
  331.    else
  332.       EnableMenuItem(hCurrentMenu, IDM_EDITPASTE, MF_GRAYED);
  333.  
  334.    /* set the Undo, Redo, Insert, and WordWrap menu items */
  335.    wStatus = (UINT)edGetUndoState(hWnd);
  336.    if (!wStatus)
  337.       wStatus = MF_GRAYED;
  338.    else
  339.       wStatus = MF_ENABLED;
  340.    EnableMenuItem(hCurrentMenu, IDM_EDITUNDO, wStatus);
  341.  
  342.    wStatus = (UINT)edGetRedoState(hWnd);
  343.    if (!wStatus)
  344.       wStatus = MF_GRAYED;
  345.    else
  346.       wStatus = MF_ENABLED;
  347.    EnableMenuItem(hCurrentMenu, IDM_EDITREDO, wStatus);
  348.  
  349.    wStatus = (UINT)edGetWordWrapState(hWnd);
  350.    if (!wStatus)
  351.       wStatus = MF_UNCHECKED;
  352.    else
  353.       wStatus = MF_CHECKED;
  354.    CheckMenuItem (hCurrentMenu, IDM_EDITTOGGLEWRAP, MF_BYCOMMAND|wStatus);
  355.  
  356.    wStatus = (UINT)edGetInsertState(hWnd);
  357.    if (!wStatus)
  358.       wStatus = MF_UNCHECKED;
  359.    else
  360.       wStatus = MF_CHECKED;
  361.    CheckMenuItem (hCurrentMenu, IDM_EDITTOGGLEINS, MF_BYCOMMAND|wStatus);
  362.  
  363.    return TRUE;   /* we handled it, don't return 0 */
  364.    }
  365.